myApp
    .controller('todayMenuCtrl', function ($cookieStore,$scope,$http,CONFIG) {

        /*根据user cookie 初始化加载菜单*/
        $scope.isAdmin = $cookieStore.get("userRole") == "admin";
        $scope.userName = $cookieStore.get("userName");

        /*初始化, 加载所有今日菜单*/
        var url = CONFIG.mainConfig.url+"/m02/u02/findAllDayMenuDetail";
        var date = new Date();
        var formatDate = date.toLocaleDateString();
        $http({
            method:'post',
            url:url,
            params:{
                today:formatDate
            }
        })
            .success(function (result,status) {
                /*alert(result.rtnCode+","+status);*/

                if (result.rtnCode == 0000000) {
                    $scope.todayMenus=result.bizData;
                }
                else {
                    alert(result.msg);
                }

            })
            .error(function () {
                alert("error");
            });

        /*加载菜单模板*/
        $scope.ifHideTemplate=true;
        $scope.showMenuTemplate = function(){
            /*加载所有菜单模板*/
            $scope.ifHideTemplate=false;
            var url = CONFIG.mainConfig.url+"/m02/u00/findAllMenu";
            $http({
                method: 'post',
                url: url
            })
                .success(function (result) {

                    if (result.rtnCode == 0000000) {
                        $scope.menuTemplates = result.bizData;
                    }
                    else {
                        alert(result.msg);
                    }

                })
                .error(function () {
                    alert("error");
                });
        };

        /*增加今日新菜*/
        $scope.addTodayMenu = function(menu_id,todayMenus){
            /*添加到DB*/
            var isExistsTodayMenu = false;
            angular.forEach(todayMenus,function(menu){
                if(menu.menu_id==menu_id){
                    isExistsTodayMenu = true;
                    alert('相同的菜单无需添加两次');
                }
            });
            if(!isExistsTodayMenu) {
                var url = CONFIG.mainConfig.url+"/m02/u02/addOneDayMenu";
                $http({
                    method: 'post',
                    url: url,
                    params: {
                        menu_id: menu_id,
                        today: formatDate
                    }
                })
                    .success(function (result) {

                        if (result.rtnCode == 0000000) {
                            //TODO:同步db数据显示到页面
                            var url = CONFIG.mainConfig.url+"/m02/u02/findAllDayMenuDetail";
                            var date = new Date();
                            var formatDate = date.toLocaleDateString();
                            $http({
                                method: 'post',
                                url: url,
                                params: {
                                    today: formatDate
                                }
                            })
                                .success(function (result, status) {

                                    if (result.rtnCode == 0000000) {
                                        $scope.todayMenus = result.bizData;
                                    }
                                    else {
                                        alert(result.msg);
                                    }

                                })
                                .error(function () {
                                    alert("error");
                                });
                        }
                        else {
                            alert(result.msg);
                        }

                    })
                    .error(function () {
                        alert("error");
                    });
            }
        };

        /*从今日菜单中删除*/
        $scope.deleteTodayMenu = function(menu_id,index){
            $scope.todayMenus.splice(index,1);
            var url = CONFIG.mainConfig.url+"/m02/u02/deleteOneDayMenu";
            $http({
                method:'post',
                url:url,
                params:{
                    menuId:menu_id
                }
            })
                .success(function (result,status) {

                    if (result.rtnCode == 0000000) {
                        //TODO:
                    }
                    else {
                        alert(result.msg);
                    }

                })
                .error(function () {
                    alert("error");
                });
        }
    });